Completed
Pull Request — master (#230)
by
unknown
04:14 queued 01:51
created

$gaOptoutLinks.click   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 11
rs 9.4285
c 1
b 0
f 0
1
/* global wpData */
2
3
import $ from 'jquery'
4
5
const $gaOptoutLinks = $('.globalAction-optoutGa')
6
const data = wpData
7
8
const alreadyOptedOut = getOptoutCookie()
9
10
if (alreadyOptedOut) {
11
  $gaOptoutLinks.remove()
12
  window['ga-disable-' + data.gaId] = true
13
} else {
14
  $gaOptoutLinks.on('click', function (e) {
15
    e.preventDefault()
16
17
    const confirmOptout = window.confirm(data.confirm)
18
19
    if (confirmOptout) {
20
      window['ga-disable-' + data.gaId] = true
21
      window.alert(data.success)
22
      setOptoutCookie()
23
    }
24
  })
25
}
26
27
function setOptoutCookie () {
28
  document.cookie = 'disableGa=true'
29
  $gaOptoutLinks.remove()
30
}
31
32
function getOptoutCookie () {
33
  const value = document.cookie
34
  const parts = value.split('; disableGa=')
35
  if (parts.length === 2) {
36
    return parts.pop().split(';').shift()
37
  } else {
38
    return false
39
  }
40
}
41